﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.Admin == "undefined") Axis.Admin = {};
Axis.Admin.User = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        var cmaskdate = new CMaskDate();
        cmaskdate.Init($("input[role=datepicker]"));
        var cdropdown = new CDropDownCheckList({ width: 200 });
        cdropdown.Init("#ddlFilter");
        var cdropdown2 = new CDropDownCheckList({ width: 200, maxDropHeight: 110, firstItemChecksAll: true });
        cdropdown2.Init("[id$='ddlProvinceFilter'],[id$='ddlEducationFilter'],[id$='ddlClassFilter'],[id$='ddlMaritalStatusFilter'],[id$='ddlEducationFilter'],[id$='ddlJobFilter'],[id$='ddlTypeFilter'],[id$='ddlGenderFilter']");
        $("#FormFilter").hide();
        thisObj.Click();
    },
    Grid: {
        ID: "#GridContents",
        PagesID: "#GridPages",
        GridURL: "Service/UserService.ashx?Action=GET",
        Instance: null,
        Init: function () {
            var thisObj = this;
            var options = {
                url: thisObj.GridURL,
                datatype: "json",
                colNames: [
                    'ID', 'FullName', 'Login Name', 'Email', 'Mobile', 'Phone', 'D-Birth',
                    'GenderName', 'Gender', 'Address', 'Ward', 'ProvinceName', 'ProvinceID'
                ],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, hidden: true, frozen: true },
                    { name: 'FullName', index: 'FullName', width: 200, frozen: true },
                    { name: 'LoginName', index: 'LoginName', width: 100 },
                    { name: 'Email', index: 'Email', width: 170, align: 'center', hidden: true, hidedlg: true },
                    { name: 'Mobile', index: 'Mobile', width: 70, align: 'center', hidden: true, hidedlg: true },
                    { name: 'Phone', index: 'Phone', width: 70, align: 'center', hidden: true, hidedlg: true },
                    { name: 'DateOfBirth', index: 'DateOfBirth', width: 60, align: 'center', hidden: true, hidedlg: true },
                    { name: 'GenderName', index: 'GenderName', width: 40, align: 'center', hidden: true, hidedlg: true },
                    { name: 'Gender', index: 'Gender', hidden: true, hidedlg: true },
                    { name: 'Address', index: 'Address', width: 200, hidden: true, hidedlg: true },
                    { name: 'Ward', index: 'Ward', width: 70, align: 'center', hidden: true, hidedlg: true },
                    { name: 'ProvinceName', index: 'ProvinceName', width: 100, hidden: true, hidedlg: true },
                    { name: 'ProvinceID', index: 'ProvinceID', hidden: true, hidedlg: true }

                ],
                onCellSelect: function (rowid, iCol, cellcontent, e) {
                },
                onSelectRow: function (rowid, status, e) {
                },
                ondblClickRow: function (rowid, iRow, iCol, e) {
                },
                multiselect: true,
                height: 'auto',
                rownumbers: true,
                autowidth: true,
                shrinkToFit: false,
                rowNum: 10,
                pager: thisObj.PagesID
            };
            thisObj.Instance = new CGrid(options);
            thisObj.Instance.Init(thisObj.ID);
            thisObj.Instance.NavGrid();
            thisObj.Instance.FrozenColumns();
        },
        Reload: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.ReloadGrid();
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.Instance) {
                return thisObj.Instance.SelectRow();
            }
            return null;
        }
    },
    DialogEdit: {
        ID: "#FormEdit",
        Dialog: null,
        Init: function (action, opts) {
            var thisObj = this;
            Axis.Admin.User.ClearForm();

            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Edit User",
                    width: 330
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
            var buttonText = "";
            switch (action) {
                case "INSERT":
                    thisObj.Dialog.SetTitle("Add New User");
                    $(thisObj.ID).find("[data-role='INSERT']").show();
                    $(thisObj.ID).find("#txtLoginName").focus();
                    buttonText = "Add";
                    break;
                case "UPDATE":
                    thisObj.Dialog.SetTitle("Edit: " + opts.LoginName);
                    $.each(opts, function (key, value) {
                        $(thisObj.ID).find("[data-field=" + key + "]").val(value);
                    });
                    buttonText = "Update";
                    break;
            }
            var buttons = [
                        {
                            text: buttonText,
                            click: function (e) {
                                Axis.Admin.User.Update(action, opts);
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Cancel",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

            ];
            $(thisObj.ID).dialog('option', 'buttons', buttons);
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        }
    },
    DialogFilter: {
        ID: "#FormFilter",
        Dialog: null,
        Init: function (type, opts) {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Filter",
                    width: 330,
                    height: 400,
                    buttons: [
                        {
                            text: "Search",
                            click: function (e) {
                                var data = {};
                                $("#FormFilter").find("input,select").each(function () {
                                    var $this = $(this);
                                    if ($this.attr("data-field")) {
                                        var key = $this.attr("data-field");
                                        var value = $this.val();
                                        if ($.isArray(value)) {
                                            data[key] = value.join(", ");
                                        }
                                        else {
                                            data[key] = value;
                                        }
                                    }
                                });
                                data = decodeURIComponent($.param(data));
                                $("#GridContents").jqGrid('setGridParam', { url: "Service/UserService.ashx?Action=GET&" + data, page: 1 }).trigger("reloadGrid");
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Cancel",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

                    ]
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        }
    },
    ClearForm: function () {
        $("#FormEdit").find("[data-field]").each(function () {
            var $this = $(this);
            $this.val("");
            if ($this.is("select")) {
                var value = $this.find("option[Default-Value='1']").attr("value");
                $this.val(value);
            }

        });
        $("#FormEdit [data-role='INSERT']").hide();
    },
    Update: function (type, opts) {
        var thisObj = this;
        var data = {};
        switch (type) {
            case "INSERT":
                $("#FormEdit").find("input,select").each(function () {
                    var $this = $(this);
                    if ($this.attr("data-field")) {
                        var key = $this.attr("data-field");
                        data[key] = $this.val();
                    }
                });
                break;
            case "UPDATE":
                $("#FormEdit").find("input,select").each(function () {
                    var $this = $(this);
                    if ($this.attr("data-field") && $this.attr("data-role") != "INSERT") {
                        var key = $this.attr("data-field");
                        data[key] = $this.val();
                    }
                });
                break;
            case "DELETE":
                data.ID = opts.ID;
                break;
        }

        data.Action = type;
        $.Ajax({
            url: 'Service/UserService.ashx',
            type: 'POST',
            data: data,
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = eval("(" + data + ")");
                    if (data.IsSuccessfull) {
                        thisObj.Grid.Reload();
                    }
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    Click: function () {
        var thisObj = this;
        $("#imgUserAdd").click(function () {
            thisObj.DialogEdit.Init("INSERT");
        });
        $("#imgUserEdit").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID)
                thisObj.DialogEdit.Init("UPDATE", rowData);
            else {
                $.CAlert.Alert({
                    Title: "Sửa tài khoản",
                    Content: "Vui lòng chọn tài khoản cần sửa.",
                });
            }
        });
        $("#imgUserDel").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID) {
                $.CAlert.Confirm({
                    Title: "Xóa tài khoản",
                    Content: $.string.Format("Bạn có chắc muốn xóa tài khoản [{0}].", rowData.Email),
                }, function (isOK) {
                    if (isOK)
                        thisObj.Update("DELETE", rowData);
                });
            }
            else {
                $.CAlert.Alert({
                    Title: "Xóa tài khoản",
                    Content: "Vui lòng chọn tài khoản cần xóa.",
                });
            }

        });
        $("#imgUserFilter").click(function () {
            thisObj.DialogFilter.Init();
        });
    }
};
$(function () {
    Axis.Admin.User.Init();
});